<html>
<head><meta charset="utf-8"><title>Why does `ToString` always `shrink_to_fit`? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html">Why does `ToString` always `shrink_to_fit`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213067938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213067938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213067938">(Oct 12 2020 at 17:51)</a>:</h4>
<p>I just spotted this in passing: <a href="https://github.com/rust-lang/rust/blob/f3ab6f05846951bed41d4b0661ac0735aebf3687/library/alloc/src/string.rs#L2197-L2205">https://github.com/rust-lang/rust/blob/f3ab6f05846951bed41d4b0661ac0735aebf3687/library/alloc/src/string.rs#L2197-L2205</a></p>
<p><code>shink_to_fit</code> is one of those things that I've always understood as something that you shouldn't usually call, since if it's short-lived it's not worth bothering, and if anyone might append to the <code>String</code> later then they'd like to have that spare capacity rather than need to re-re-allocate space.  That it should really only be used if you have reason to expect that it was once much longer than it is now, and that you're going to keep it around for a while without editing it.  (Though in Rust I'd probably to say to use <code>Box&lt;str&gt;</code> in that case instead of <code>String</code> anyway...)</p>
<p>Given that, anyone know <em>why</em> it's called in <code>ToString::to_string</code>?  Or am I misunderstanding when I should be using <code>shrink_to_fit</code>...</p>



<a name="213485285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213485285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213485285">(Oct 15 2020 at 20:46)</a>:</h4>
<p>I agree it should probably not be called there. Might be good to try to get actual statistics on this, but I'd guess that to_string() is used more often as a temporary or as part of building something bigger than it is used as the final result.</p>
<p>E.g. <code>println!("{}", thing.to_string().to_lowercase())</code>, where shrink_to_fit() is just a waste of time. Or another example from rustc: <a href="https://github.com/rust-lang/rust/blob/b5c9e2448c9ace53ad5c11585803894651b18b0a/compiler/rustc_ast/src/util/literal.rs#L242-L244">https://github.com/rust-lang/rust/blob/b5c9e2448c9ace53ad5c11585803894651b18b0a/compiler/rustc_ast/src/util/literal.rs#L242-L244</a></p>
<p>I wonder if removing that shrink_to_fit would cause any visible performance difference in rustc. Might be worth a try, just to have some data.</p>



<a name="213486041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213486041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213486041">(Oct 15 2020 at 20:52)</a>:</h4>
<p>I bet there's definitely enough to_string() use in rustc for a perf run to be interesting.</p>



<a name="213490733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213490733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213490733">(Oct 15 2020 at 21:33)</a>:</h4>
<p>I'd love to see that perf run. We should check both memory usage and performance.</p>



<a name="213490844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213490844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213490844">(Oct 15 2020 at 21:34)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> <span class="user-mention" data-user-id="243558">@Steven Fackler</span> If one of you would be up for submitting that as a quick PR, I'll happily throw it at rust-timer.</p>



<a name="213491213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491213">(Oct 15 2020 at 21:37)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/77997">https://github.com/rust-lang/rust/pull/77997</a></p>



<a name="213491216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491216">(Oct 15 2020 at 21:37)</a>:</h4>
<p>FWIW, the shrink was added without comment in <a href="https://github.com/rust-lang/rust/pull/20377">https://github.com/rust-lang/rust/pull/20377</a></p>



<a name="213491375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491375">(Oct 15 2020 at 21:39)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> Awesome; queued.</p>



<a name="213491410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491410">(Oct 15 2020 at 21:39)</a>:</h4>
<p>This has the potential to substantially reduce the number of times we hit the allocator.</p>



<a name="213491438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491438">(Oct 15 2020 at 21:39)</a>:</h4>
<p>And if there are regressions, we may be able to fix them up by adding targeted <code>.shrink_to_fit()</code> calls to specific cases.</p>



<a name="213491633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491633">(Oct 15 2020 at 21:41)</a>:</h4>
<p>i've been looking through <a href="http://grep.app">grep.app</a> for usages of .to_string(). ignoring all the str::to_string()s, i see almost only cases where shrink_to_fit should not happen. (e.g. <code>f(thing.to_string().as_str())</code> and other things like the examples i mentioned before.)</p>



<a name="213491947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491947">(Oct 15 2020 at 21:44)</a>:</h4>
<p>(also a surprising number of <code>println!("... {} ...", thing.to_string())</code>)</p>



<a name="213491982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491982">(Oct 15 2020 at 21:45)</a>:</h4>
<p><span class="user-mention" data-user-id="310399">@Mara</span> That last one should just be deleted, if we can do that in a systematic way. :)</p>



<a name="213491996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213491996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213491996">(Oct 15 2020 at 21:45)</a>:</h4>
<p>/me wonders if there was some specific pattern that led to that originally.</p>



<a name="213492010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492010">(Oct 15 2020 at 21:45)</a>:</h4>
<p>oh i mean all across github. not the rust repo itself.</p>



<a name="213492019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492019">(Oct 15 2020 at 21:45)</a>:</h4>
<p>Ah.</p>



<a name="213492033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492033">(Oct 15 2020 at 21:45)</a>:</h4>
<p>/me tests something...</p>



<a name="213492142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492142">(Oct 15 2020 at 21:46)</a>:</h4>
<p>Neither rustc nor clippy has a lint for that.</p>



<a name="213492164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492164">(Oct 15 2020 at 21:46)</a>:</h4>
<p>heh, interesting</p>



<a name="213492173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492173">(Oct 15 2020 at 21:46)</a>:</h4>
<p>/me adds to the never-ending todo list</p>



<a name="213492197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492197">(Oct 15 2020 at 21:47)</a>:</h4>
<p>I can't think of <em>any</em> case where print/println/write/writeln/etc should ever have a call to <code>.to_string()</code> as the last thing that happens in one of its arguments.</p>



<a name="213492227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492227">(Oct 15 2020 at 21:47)</a>:</h4>
<p>I think that'd be safe to add to rustc as a warn-by-default lint.</p>



<a name="213492234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492234">(Oct 15 2020 at 21:47)</a>:</h4>
<p>Can you think of <em>any</em> possible false positives?</p>



<a name="213492314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492314">(Oct 15 2020 at 21:48)</a>:</h4>
<p>.to_string() is not always ToString::to_string().  some types have their own .to_string(), which is sometimes the only way to display them, maybe?</p>



<a name="213492324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492324">(Oct 15 2020 at 21:48)</a>:</h4>
<p>Note that <code>ToString</code> is specialized, so this shrinking only applies to the default <code>T: Display</code> implementation</p>



<a name="213492341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492341">(Oct 15 2020 at 21:48)</a>:</h4>
<p>all the string-like <code>ToString</code> specializations will avoid this</p>



<a name="213492401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492401">(Oct 15 2020 at 21:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="310399">Mara</span> <a href="#narrow/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F/near/213492314">said</a>:</p>
<blockquote>
<p>.to_string() is not always ToString::to_string().  some types have their own .to_string(), which is sometimes the only way to display them, maybe?</p>
</blockquote>
<p>The lint should be tied to the specific function, not the name <code>to_string</code>.</p>



<a name="213492515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492515">(Oct 15 2020 at 21:50)</a>:</h4>
<p>We could add a <code>#[rustc_lint_as_display_arg]</code> attribute, to be applied to functions that go from one thing implementing<code>Display</code> to another thing implementing <code>Display</code> in a way that shouldn't change the output.</p>



<a name="213492544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492544">(Oct 15 2020 at 21:50)</a>:</h4>
<p>yeah. but for ToString::to_string.. i can't think of any time where you'd actually want to use it as a formatting argument.</p>



<a name="213492561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492561">(Oct 15 2020 at 21:50)</a>:</h4>
<p>or hm. i can</p>



<a name="213492587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492587">(Oct 15 2020 at 21:50)</a>:</h4>
<p>with <code>{:&gt;40}</code> or something. Strings apply the padding nicely. most type's Display implementations don't</p>



<a name="213492588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492588">(Oct 15 2020 at 21:50)</a>:</h4>
<p>you might want <code>to_string</code> for formats with width, when the type's own formatting doesn't support that</p>



<a name="213492612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492612">(Oct 15 2020 at 21:51)</a>:</h4>
<p>so it also should check for just <code>{}</code> or <code>{0}</code> or something.</p>



<a name="213492622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492622">(Oct 15 2020 at 21:51)</a>:</h4>
<p>yeah, what <span class="user-mention" data-user-id="138448">@cuviper</span> is saying.</p>



<a name="213492651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492651">(Oct 15 2020 at 21:51)</a>:</h4>
<p>That'd be a bug in the Display impl, but yeah, flagging code that's working around that would be a false positive.</p>



<a name="213492758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492758">(Oct 15 2020 at 21:52)</a>:</h4>
<p>We could account for that in the lint, though, as you said.</p>



<a name="213492759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492759">(Oct 15 2020 at 21:52)</a>:</h4>
<p>i bet the majority of Display implementations don't respect padding. most just do a few write!()s</p>



<a name="213492894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492894">(Oct 15 2020 at 21:53)</a>:</h4>
<p>hm, i vaguely remember seeing one implementation that would check if any padding is active, and then call <code>f.pad(&amp;self.to_string())</code> to take care of that.</p>



<a name="213492904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492904">(Oct 15 2020 at 21:53)</a>:</h4>
<p>heh</p>



<a name="213492910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492910">(Oct 15 2020 at 21:53)</a>:</h4>
<p>It's unfortunate that, for the benefit of a small handful of types that'll want to do something unusual with modifiers, we make all Display impls handle modifiers themselves.</p>



<a name="213492916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213492916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213492916">(Oct 15 2020 at 21:53)</a>:</h4>
<p>(another case where shrink_to_fit would be bad btw :P)</p>



<a name="213493041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493041">(Oct 15 2020 at 21:54)</a>:</h4>
<p>yeah. could be done differently, but in practice it doesn't really seem like much of a problem.</p>



<a name="213493051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493051">(Oct 15 2020 at 21:55)</a>:</h4>
<p>found it! <a href="https://github.com/rust-lang/rust/blob/dd7fc54ebdca419ad9d3ab1e9f5ed14e770768ea/compiler/rustc_data_structures/src/svh.rs#L45-L49">https://github.com/rust-lang/rust/blob/dd7fc54ebdca419ad9d3ab1e9f5ed14e770768ea/compiler/rustc_data_structures/src/svh.rs#L45-L49</a></p>



<a name="213493084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493084">(Oct 15 2020 at 21:55)</a>:</h4>
<p>haha, nice</p>



<a name="213493168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493168">(Oct 15 2020 at 21:56)</a>:</h4>
<p>i wonder if that type is ever Displayed anywhere with formatting/padding options.</p>



<a name="213493189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493189">(Oct 15 2020 at 21:56)</a>:</h4>
<p>i feel like a <code>write!(f, "{:016x}", self.hash)</code> would've been just fine</p>



<a name="213493193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493193">(Oct 15 2020 at 21:56)</a>:</h4>
<p>This seems like an excellent argument for <code>#[derive(Display)]</code>.</p>



<a name="213493212"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493212" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493212">(Oct 15 2020 at 21:56)</a>:</h4>
<p>(because we can make derived implementations handle padding correctly)</p>



<a name="213493241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493241">(Oct 15 2020 at 21:57)</a>:</h4>
<p>hm, how would that derive know to use <code>"{:016x}", self.hash</code>?</p>



<a name="213493339"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493339" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493339">(Oct 15 2020 at 21:58)</a>:</h4>
<p>or only have derive(Display) for wrapper types with a single member?</p>



<a name="213493345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493345">(Oct 15 2020 at 21:58)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Copy, Clone, PartialEq, Eq, Debug, Display)]</span><span class="w"></span>
<span class="cp">#[display(</span><span class="s">"{hash:016x}"</span><span class="cp">)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Svh</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">hash</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="213493438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493438">(Oct 15 2020 at 21:59)</a>:</h4>
<p>Similar syntax to <a href="https://docs.rs/thiserror/">thiserror</a>, just with <code>display</code> instead of <code>error</code> as the attribute name.</p>



<a name="213493495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493495">(Oct 15 2020 at 22:00)</a>:</h4>
<p>yeah okay. but i guess it's a bit too late, considering almost all Display implementations already ignore their padding options. and the derive couldn't easily do anything else than <code>f.pad(thing.to_string())</code> in most cases. it'd have to be very smart to realize that <code>{:016x}</code> on a u64 is always going to be 16 chars, and that calculating the padding/alignment can be done without allocating/formatting first.</p>



<a name="213493574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493574">(Oct 15 2020 at 22:01)</a>:</h4>
<p>As an initial step, I wonder if it'd make sense to have a <code>pad_fmt</code> method on <code>Formatter</code>.</p>



<a name="213493613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493613">(Oct 15 2020 at 22:01)</a>:</h4>
<p>Something that takes a <code>std::fmt::Arguments</code> in place of a <code>&amp;str</code>.</p>



<a name="213493682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493682">(Oct 15 2020 at 22:02)</a>:</h4>
<p><code>f.pad_fmt(format_args!("{:016x}", self.hash))</code></p>



<a name="213493684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493684">(Oct 15 2020 at 22:02)</a>:</h4>
<p>maybe a Display::fmt_length that gives the number of chars (bytes?) that fmt is going to produce. the default implementation could just fmt the thing to some object that just counts the length, i suppose.</p>



<a name="213493732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493732">(Oct 15 2020 at 22:03)</a>:</h4>
<p>but what would that pad_fmt do, other than simply <code>f.pad(args.to_string())</code>?</p>



<a name="213493764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493764">(Oct 15 2020 at 22:03)</a>:</h4>
<p>hm, it could skip the to_string() if there are no options specified. but other than that, i don't think it can really help much?</p>



<a name="213493838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493838">(Oct 15 2020 at 22:04)</a>:</h4>
<p>I was imagining a way to get the length of an <code>Arguments</code> without (always) fully formatting it.</p>



<a name="213493870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493870">(Oct 15 2020 at 22:04)</a>:</h4>
<p>yeah, okay</p>



<a name="213493936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493936">(Oct 15 2020 at 22:05)</a>:</h4>
<p>Same concept as your mention of <code>fmt_length</code>, just on <code>Arguments</code> rather than <code>Display</code>.</p>



<a name="213493960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213493960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213493960">(Oct 15 2020 at 22:06)</a>:</h4>
<p>They'd work well together, in that <code>Arguments::len</code> could call <code>fmt_length</code> for anything it can't compute without asking the type.</p>



<a name="213494008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494008">(Oct 15 2020 at 22:06)</a>:</h4>
<p>arguments wlil have to call fmt_length on all of its formatters</p>



<a name="213494020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494020">(Oct 15 2020 at 22:06)</a>:</h4>
<p>and Arguments already implements Display, so would be the same method</p>



<a name="213494040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494040">(Oct 15 2020 at 22:06)</a>:</h4>
<p>Oh, good point!</p>



<a name="213494049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494049">(Oct 15 2020 at 22:06)</a>:</h4>
<p>No need for a separate method, it has the same semantics.</p>



<a name="213494082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494082">(Oct 15 2020 at 22:07)</a>:</h4>
<p>god all this formatting stuff is a never ending rabbit hole, isn't it? :P  every time there's some conversation about  anything related to std::fmt, so many new problems and interesting ideas pop up ^^'</p>



<a name="213494128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494128">(Oct 15 2020 at 22:07)</a>:</h4>
<p>/me adds Display::fmt_length to the never-ending to do list.</p>



<a name="213494129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494129">(Oct 15 2020 at 22:08)</a>:</h4>
<p>I suppose that format args provide a <em>minimum</em> width, not a <em>maximum</em> width, so technically it'd be necessary to call <code>fmt_length</code> there. :(</p>



<a name="213494172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494172">(Oct 15 2020 at 22:08)</a>:</h4>
<p>Although...</p>



<a name="213494195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494195">(Oct 15 2020 at 22:08)</a>:</h4>
<p>I wonder if <code>Display</code> should also have an associated non-method const function <code>max_fmt_length</code> that returns <code>Option&lt;usize&gt;</code>.</p>



<a name="213494245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494245">(Oct 15 2020 at 22:09)</a>:</h4>
<p>and then the same for Binary, LowerHex, UpperHex, etc. etc.? :(</p>



<a name="213494251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494251">(Oct 15 2020 at 22:09)</a>:</h4>
<p>Yup.</p>



<a name="213494337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494337">(Oct 15 2020 at 22:10)</a>:</h4>
<p>yeah, maybe. then u64 could clearly say it's never more than 16 hex digits.</p>



<a name="213494342"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494342" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494342">(Oct 15 2020 at 22:10)</a>:</h4>
<p>Exactly.</p>



<a name="213494374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494374">(Oct 15 2020 at 22:10)</a>:</h4>
<p>Then, entirely at compile time, <code>format_args!("{:016x}", thing).fmt_len()</code> could confirm that <code>thing</code> can never be wider than <code>16</code>, so it doesn't need to call <code>thing.fmt_len()</code>.</p>



<a name="213494408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494408">(Oct 15 2020 at 22:11)</a>:</h4>
<p>i'd love to talk all night about std::fmt. but i should go to sleep ^^</p>



<a name="213494412"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494412" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494412">(Oct 15 2020 at 22:11)</a>:</h4>
<p>Fair. :)</p>



<a name="213494423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213494423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213494423">(Oct 15 2020 at 22:11)</a>:</h4>
<p>Meanwhile, I hope your PR is a huge win. :)</p>



<a name="213495680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213495680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213495680">(Oct 15 2020 at 22:26)</a>:</h4>
<p>Thanks for making a PR here, <span class="user-mention" data-user-id="310399">@Mara</span>!</p>
<p>Hmm, is it even feasible to implement your own padding-and-such logic here?  I vaguely remember some old conversation about the flags not even being usefully exposed...</p>



<a name="213496909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213496909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213496909">(Oct 15 2020 at 22:41)</a>:</h4>
<p>I <em>think</em> they're exposed, but I really can't imagine many scenarios where you'd <em>want</em> to hand-implement it yourself.</p>



<a name="213497547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213497547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213497547">(Oct 15 2020 at 22:49)</a>:</h4>
<p>Ah, I'm remembering an ancient conversation, looks like this is better since 1.24 <a href="https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.flags">https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.flags</a></p>



<a name="213532243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213532243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213532243">(Oct 16 2020 at 08:32)</a>:</h4>
<p>There are still some secret flags, like <code>hex</code> for Debug.</p>



<a name="213533065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213533065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213533065">(Oct 16 2020 at 08:41)</a>:</h4>
<p><code>DebugLowerHex</code> and <code>DebugUpperHex</code> are the only two secret flags it seems.</p>



<a name="213536172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213536172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213536172">(Oct 16 2020 at 09:13)</a>:</h4>
<p>Removing shrink_to_fit perf results: <a href="https://perf.rust-lang.org/compare.html?start=dd7fc54ebdca419ad9d3ab1e9f5ed14e770768ea&amp;end=a9f657d64df859414475bb3329cda65b5445193e">https://perf.rust-lang.org/compare.html?start=dd7fc54ebdca419ad9d3ab1e9f5ed14e770768ea&amp;end=a9f657d64df859414475bb3329cda65b5445193e</a></p>



<a name="213538198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213538198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213538198">(Oct 16 2020 at 09:36)</a>:</h4>
<p>do we know whether the -doc benchmarks still have high variance as before ?</p>



<a name="213557431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213557431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213557431">(Oct 16 2020 at 13:10)</a>:</h4>
<p>Yes, and they will until rustdoc stops loading external crates on startup</p>



<a name="213557507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213557507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213557507">(Oct 16 2020 at 13:11)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/68427">https://github.com/rust-lang/rust/issues/68427</a></p>



<a name="213558672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Why%20does%20%60ToString%60%20always%20%60shrink_to_fit%60%3F/near/213558672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Why.20does.20.60ToString.60.20always.20.60shrink_to_fit.60.3F.html#213558672">(Oct 16 2020 at 13:20)</a>:</h4>
<p>Fixing that needs a rewrite of intra-doc links which I don't currently have time for</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>